﻿<%@ WebHandler Language="C#"  Class="WeixinApi" %>

    using Chain.BLL;
    using Chain.Common.DEncrypt;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Xml;
    using Web.Common;

    public partial class WeixinApi : IHttpHandler
    {
        private string Content = string.Empty;
        private HttpContext Context;
        private string FromUserName = string.Empty;
        private Chain.BLL.Mem memBll = new Chain.BLL.Mem();
        private Chain.Model.Mem memModel = new Chain.Model.Mem();
        private string MsgType = string.Empty;
        private string postStr = string.Empty;
        private HttpRequest Request;
        private HttpResponse Response;
        private Chain.Model.WeiXinRule ruleModel = new Chain.Model.WeiXinRule();
        private string sendXML = string.Empty;
        private HttpServerUtility Server;
        protected string token = string.Empty;
        private Chain.BLL.WeiXinLog weiXinLogBLL = new Chain.BLL.WeiXinLog();
        private Chain.Model.WeiXinLog weiXinLogModel = new Chain.Model.WeiXinLog();
        private Chain.BLL.WeiXinRule weiXinRuleBLL = new Chain.BLL.WeiXinRule();
        private XmlDocument xmlDoc = new XmlDocument();

        private void ApplicationMemberCard()
        {
            Chain.Model.Mem mem = this.memBll.GetMemByWeiXinCard(this.FromUserName);
            if (mem == null)
            {
                this.sendXML = ResponseSendStr.Text(this.postStr, "您已进入会员申请模式," + this.weiXinRuleBLL.Reply1());
                this.GiveWeiXinLodAssignment("", 0, "0", "", 0);
                this.weiXinLogBLL.Add(this.weiXinLogModel);
            }
            else
            {
                string NewsDesc = string.IsNullOrEmpty(PubFunction.curParameter.strWeiXinShopName) ? "" : PubFunction.curParameter.strWeiXinShopName;
                NewsDesc = (NewsDesc == "") ? "您的微信会员卡" : ("您的" + NewsDesc + "微信会员卡");
                this.Card(mem, "会员卡查询", NewsDesc);
            }
        }

        private void AttentionNews(string postStr)
        {
            switch (this.xmlDoc.GetElementsByTagName("Event")[0].InnerText)
            {
                case "subscribe":
                {
                    StringBuilder tempSb = new StringBuilder();
                    tempSb.AppendLine(PubFunction.curParameter.strWeiXinSalutatory).AppendLine().AppendLine(this.weiXinRuleBLL.AttentionStr());
                    this.sendXML = ResponseSendStr.Text(postStr, tempSb.ToString());
                    break;
                }
                case "CLICK":
                    this.MenuClick();
                    break;
            }
        }

        private void Card(Chain.Model.Mem mem, string NewsTitle, string NewsDesc)
        {
            this.DrawImage(mem, this.FromUserName);
            string NewsUrlFirst = string.Concat(new object[] { "http://", PubFunction.curParameter.strDoMain, "/Upload/WeiXin/Images/", this.FromUserName, ".jpg?id=", Guid.NewGuid() });
            string NewsUrlSecond = "http://" + PubFunction.curParameter.strDoMain + "/ReceptionPage/index.aspx?MemWeiXinCard=" + this.FromUserName;
            List<Chain.Model.WeiXinNews> newsList = this.GetModel(NewsTitle, NewsDesc, NewsUrlFirst, NewsUrlSecond);
            this.sendXML = ResponseSendStr.News(this.postStr, newsList);
        }

        private void ConvertWeiXinMember(string postStr)
        {
            if (this.memBll.GetMemByWeiXinCard(this.FromUserName) != null)
            {
                this.sendXML = ResponseSendStr.Text(postStr, "您已是微信会员");
            }
            else
            {
                this.sendXML = ResponseSendStr.Text(postStr, "您已进入转微信会员模式," + this.weiXinRuleBLL.Reply2());
                this.GiveWeiXinLodAssignment("", 0, "2", "", 0);
                this.weiXinLogBLL.Add(this.weiXinLogModel);
            }
        }

        private void Do0ForApplicationMember()
        {
            this.Content = this.xmlDoc.GetElementsByTagName("Content")[0].InnerText.Trim();
            if (!Regex.IsMatch(this.Content, @"^1[3|4|5|8]\d{9}$"))
            {
                this.GiveWeiXinLodAssignment("", 0, "0", "", this.weiXinLogModel.ErrorTimes + 1);
                this.ErrorNewsHandle("由于您的误操作次数过多,会员申请模式已退出", "您发送的手机号格式有误,请重新发送");
            }
            else if (this.memBll.GetModelByMemMobile(this.Content) != null)
            {
                this.sendXML = ResponseSendStr.Text(this.postStr, "该手机号已被注册,请重新输入一个新的手机号");
            }
            else if (PubFunction.curParameter.bolWeiXinSMSVcode)
            {
                if (PubFunction.curParameter.bolSms)
                {
                    if (SMSInfo.GetBalance(false) == "0")
                    {
                        this.sendXML = ResponseSendStr.Text(this.postStr, "商家短信不足,发送短信验证码失败,请与商家联系");
                        this.weiXinLogBLL.Delete(this.FromUserName);
                    }
                    else
                    {
                        string smsVCode = this.GetRandomCode();
                        string NewsDesc = string.IsNullOrEmpty(PubFunction.curParameter.strWeiXinShopName) ? "" : PubFunction.curParameter.strWeiXinShopName;
                        NewsDesc = "温馨提示,欢迎您注册" + NewsDesc + "微信会员,您的" + NewsDesc + "微信会员短信验证码是：" + smsVCode;
                        this.sendXML = ResponseSendStr.Text(this.postStr, "请发送短信验证码");
                        SMSInfo.Send_GXSMS(false, this.Content, NewsDesc, "");
                        Chain.Model.SmsLog modelSms = new Chain.Model.SmsLog();
                        modelSms.SmsMemID = 0;
                        modelSms.SmsMobile = this.Content;
                        modelSms.SmsContent = NewsDesc;
                        modelSms.SmsTime = DateTime.Now;
                        modelSms.SmsShopID = 1;
                        modelSms.SmsUserID = 1;
                        modelSms.SmsAmount = PubFunction.GetSmsAmount(NewsDesc);
                        modelSms.SmsAllAmount = modelSms.SmsAmount;
                        new Chain.BLL.SmsLog().Add(modelSms);
                        this.GiveWeiXinLodAssignment(this.Content, 2, "1", smsVCode, 0);
                        this.weiXinLogBLL.Update(this.weiXinLogModel);
                    }
                }
                else
                {
                    this.sendXML = ResponseSendStr.Text(this.postStr, "系统短信功能暂未开启,发送短信验证码失败,请与商家联系");
                    this.weiXinLogBLL.Delete(this.FromUserName);
                }
            }
            else
            {
                int isOk = this.MemRegister(false);
                if (isOk > 0)
                {
                    this.DoCard("恭喜您会员卡办理成功");
                }
                else
                {
                    string strErr = "系统错误，会员办卡失败！";
                    switch (isOk)
                    {
                        case -2:
                            strErr = "手机号码重复，会员办卡失败！";
                            break;

                        case -1:
                            strErr = "会员卡号重复，会员办卡失败！";
                            break;

                        case -6:
                            strErr = "卡面号重复，会员办卡失败！";
                            break;
                    }
                    this.sendXML = ResponseSendStr.Text(this.postStr, strErr);
                }
            }
        }

        private void Do1ForApplicationMemberByReplyTelNumber()
        {
            this.Content = this.xmlDoc.GetElementsByTagName("Content")[0].InnerText.Trim();
            if (PubFunction.curParameter.bolWeiXinSMSVcode && !(this.weiXinLogModel.RandomCode == this.Content))
            {
                this.GiveWeiXinLodAssignment(this.weiXinLogModel.RecordContent, this.weiXinLogModel.RecordContentType, this.weiXinLogModel.StatusCode, this.weiXinLogModel.RandomCode, this.weiXinLogModel.ErrorTimes + 1);
                this.ErrorNewsHandle("由于您的误操作次数过多,会员申请模式已退出", "您发送的短信验证码有误,请重新发送");
            }
            else
            {
                int isOk = this.MemRegister(true);
                if (isOk > 0)
                {
                    this.DoCard("恭喜您会员卡办理成功");
                }
                else
                {
                    string strErr = "系统错误，会员办卡失败！";
                    switch (isOk)
                    {
                        case -2:
                            strErr = "手机号码重复，会员办卡失败！";
                            break;

                        case -1:
                            strErr = "会员卡号重复，会员办卡失败！";
                            break;

                        case -6:
                            strErr = "卡面号重复，会员办卡失败！";
                            break;
                    }
                    this.sendXML = ResponseSendStr.Text(this.postStr, strErr);
                }
            }
        }

        private void Do2MemberTransform()
        {
            this.Content = this.xmlDoc.GetElementsByTagName("Content")[0].InnerText.Trim();
            this.memModel = this.memBll.GetModelByMemCard(this.Content);
            if (this.memModel != null)
            {
                this.sendXML = ResponseSendStr.Text(this.postStr, "请发送您的会员卡密码,如果密码为空请回复 # 号");
                this.GiveWeiXinLodAssignment(this.memModel.MemCard, 1, "3", "", 0);
                this.weiXinLogBLL.Update(this.weiXinLogModel);
            }
            else
            {
                Chain.Model.Mem mem = this.memBll.GetModelByMemMobile(this.Content);
                if (mem != null)
                {
                    if (PubFunction.curParameter.bolWeiXinSMSVcode)
                    {
                        if (PubFunction.curParameter.bolSms)
                        {
                            if (SMSInfo.GetBalance(false) == "0")
                            {
                                string strTemplet = string.Format("由于本系统短信剩余条数不足,无法给您发送短信验证码;\r\n您的会员卡号为:{0}****{1},请发送您的会员卡密码,如果密码为空请回复 # 号", mem.MemCard.Substring(0, 1), mem.MemCard.Substring(mem.MemCard.Length - 1, 1));
                                this.sendXML = ResponseSendStr.Text(this.postStr, strTemplet);
                                this.GiveWeiXinLodAssignment(mem.MemCard, 1, "3", "", 0);
                                this.weiXinLogBLL.Update(this.weiXinLogModel);
                            }
                            else
                            {
                                string smsVCode = this.GetRandomCode();
                                string NewsDesc = string.IsNullOrEmpty(PubFunction.curParameter.strWeiXinShopName) ? "" : PubFunction.curParameter.strWeiXinShopName;
                                NewsDesc = "温馨提示,欢迎您申请从店铺会员转" + NewsDesc + "微信会员,您的" + NewsDesc + "微信会员短信验证码是：" + smsVCode;
                                this.sendXML = ResponseSendStr.Text(this.postStr, "请发送短信验证码");
                                SMSInfo.Send_GXSMS(false, this.Content, NewsDesc, "");
                                Chain.Model.SmsLog modelSms = new Chain.Model.SmsLog();
                                modelSms.SmsMemID = Convert.ToInt32(mem.MemID);
                                modelSms.SmsMobile = this.Content;
                                modelSms.SmsContent = NewsDesc;
                                modelSms.SmsTime = DateTime.Now;
                                modelSms.SmsShopID = 1;
                                modelSms.SmsUserID = 1;
                                modelSms.SmsAmount = PubFunction.GetSmsAmount(NewsDesc);
                                modelSms.SmsAllAmount = modelSms.SmsAmount;
                                new Chain.BLL.SmsLog().Add(modelSms);
                                this.GiveWeiXinLodAssignment(this.Content, 2, "3", smsVCode, 0);
                                this.weiXinLogBLL.Update(this.weiXinLogModel);
                            }
                        }
                        else
                        {
                            string strTemplet = string.Format("由于本系统短信功能暂未开启,无法给您发送短信验证码;\r\n您的会员卡号为:{0}****{1},请发送您的会员卡密码,如果密码为空请回复 # 号", mem.MemCard.Substring(0, 1), mem.MemCard.Substring(mem.MemCard.Length - 1, 1));
                            this.sendXML = ResponseSendStr.Text(this.postStr, strTemplet);
                            this.GiveWeiXinLodAssignment(mem.MemCard, 1, "3", "", 0);
                            this.weiXinLogBLL.Update(this.weiXinLogModel);
                        }
                    }
                    else
                    {
                        this.memModel = this.memBll.GetMemInfoByMobile(this.Content);
                        this.memModel.MemWeiXinCard = this.FromUserName;
                        this.memBll.Update(this.memModel);
                        this.DoCard("恭喜您已转移成微信会员");
                    }
                }
                else
                {
                    this.GiveWeiXinLodAssignment(this.weiXinLogModel.RecordContent, this.weiXinLogModel.RecordContentType, this.weiXinLogModel.StatusCode, this.weiXinLogModel.RandomCode, this.weiXinLogModel.ErrorTimes + 1);
                    this.ErrorNewsHandle("由于您的错误操作次数过多,转微信会员模式已退出", "您发送的手机号或卡号在系统中没找着,请重新发送");
                }
            }
        }

        private void Do3ForMemberTransformByReplyTelNumberOrMemberCard()
        {
            string Content = this.xmlDoc.GetElementsByTagName("Content")[0].InnerText.Trim();
            if (this.weiXinLogModel.RecordContentType == 1)
            {
                string pwd = DESEncrypt.Encrypt((Content == "#") ? "" : ((Content == "＃") ? "" : Content));
                this.memModel = this.memBll.GetModelByMemCard(this.weiXinLogModel.RecordContent);
                if (pwd == this.memModel.MemPassword)
                {
                    this.memModel.MemWeiXinCard = this.weiXinLogModel.MemWeiXinCard;
                    this.memBll.Update(this.memModel);
                    this.DoCard("恭喜您已转移成微信会员");
                }
                else
                {
                    this.GiveWeiXinLodAssignment(this.weiXinLogModel.RecordContent, 1, "3", "", this.weiXinLogModel.ErrorTimes + 1);
                    this.ErrorNewsHandle("由于您的错误操作次数过多,转微信会员模式已退出", "您发送的会员卡密码不正确,请重新发送");
                }
            }
            else if (!PubFunction.curParameter.bolWeiXinSMSVcode || (this.weiXinLogModel.RandomCode == Content))
            {
                this.memModel = this.memBll.GetMemInfoByMobile(this.weiXinLogModel.RecordContent);
                this.memModel.MemWeiXinCard = this.weiXinLogModel.MemWeiXinCard;
                this.memBll.Update(this.memModel);
                this.DoCard("恭喜您已转移成微信会员");
            }
            else
            {
                this.GiveWeiXinLodAssignment(this.weiXinLogModel.RecordContent, 2, this.weiXinLogModel.StatusCode, this.weiXinLogModel.RandomCode, this.weiXinLogModel.ErrorTimes + 1);
                this.ErrorNewsHandle("由于您的错误操作次数过多,转微信会员模式已退出", "您发送的短信验证码有误,请重新发送");
            }
        }

        private void DoCard(string NewsTitle)
        {
            Chain.Model.Mem mem = this.memBll.GetMemByWeiXinCard(this.FromUserName);
            string NewsDesc = string.IsNullOrEmpty(PubFunction.curParameter.strWeiXinShopName) ? "" : PubFunction.curParameter.strWeiXinShopName;
            NewsDesc = (NewsDesc == "") ? "您的微信会员卡" : ("您的" + NewsDesc + "微信会员卡");
            this.Card(mem, NewsTitle, NewsDesc);
            this.weiXinLogBLL.Delete(this.FromUserName);
        }

        public void DrawImage(Chain.Model.Mem mem, string FromUserName)
        {
            Bitmap smallWeiXin = new Bitmap(130, 130);
            Image weixinImg = QRCodeImage.CreateQRCode(mem.MemCard);
            Graphics.FromImage(smallWeiXin).DrawImage(weixinImg, new Point(-35, -35));
            string savePath = this.Server.MapPath("~/Upload/WeiXin/Images/" + FromUserName + ".jpg");
            Bitmap bigWeiXin = new Bitmap(200, 200);
            Graphics.FromImage(bigWeiXin).DrawImage(smallWeiXin, new Rectangle(0, 0, 200, 200), new Rectangle(0, 0, 130, 130), GraphicsUnit.Pixel);
            Image bgImg = Image.FromFile(this.Server.MapPath("~/Upload/WeiXin/Images/bg.jpg"), true);
            Bitmap bmp = new Bitmap(bgImg.Width, bgImg.Height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bmp);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.DrawImage(bgImg, new Rectangle(0, 0, bgImg.Width, bgImg.Height), new Rectangle(0, 0, bgImg.Width, bgImg.Height), GraphicsUnit.Pixel);
            g.DrawImage(bigWeiXin, new RectangleF(50f, 60f, 200f, 200f), new RectangleF(0f, 0f, 200f, 200f), GraphicsUnit.Pixel);
            g.DrawString("NO:" + mem.MemCard, new Font("微软雅黑", 34f, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, new PointF(280f, 70f));
            g.DrawString("积分:" + mem.MemCard, new Font("微软雅黑", 30f, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, new PointF(280f, 115f));
            g.DrawString("余额:" + mem.MemMoney.ToString("F2"), new Font("微软雅黑", 30f, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, new PointF(280f, 160f));
            string pastStr = (mem.MemPastTime.ToString("yyyy-MM-dd") == "2900-01-01") ? "有效期:永久有效" : ("有效期:" + mem.MemPastTime.ToString("yyyy-MM-dd"));
            g.DrawString(pastStr, new Font("微软雅黑", 30f, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, new PointF(280f, 205f));
            bmp.Save(savePath, ImageFormat.Jpeg);
        }

        private void ErrorNewsHandle(string errorTimesExceed, string errorTimesNoExceed)
        {
            if (this.weiXinLogModel.ErrorTimes >= 3)
            {
                this.sendXML = ResponseSendStr.Text(this.postStr, errorTimesExceed);
                this.weiXinLogBLL.Delete(this.FromUserName);
            }
            else
            {
                this.sendXML = ResponseSendStr.Text(this.postStr, errorTimesNoExceed);
                this.weiXinLogBLL.Update(this.weiXinLogModel);
            }
        }

        private string GetAllParameter()
        {
            string Para = string.Empty + "{QueryString}";
            for (int i = 0; i < this.Request.QueryString.AllKeys.Length; i++)
            {
                string name1 = Para;
                Para = name1 + this.Request.QueryString.Keys[i] + ":" + this.Request.QueryString[this.Request.QueryString.Keys[i]].ToString() + ";";
            }
            Para = Para + "{Form}";
            for (int i = 0; i < this.Request.Form.AllKeys.Length; i++)
            {
                string name15 = Para;
                Para = name15 + this.Request.Form.Keys[i] + ":" + this.Request.Form[this.Request.Form.Keys[i]].ToString() + ";";
            }
            return Para;
        }

        public List<Chain.Model.WeiXinNews> GetModel(string NewsTitle, string NewsDesc, string NewsUrlFirst, string NewsUrlSecond)
        {
            List<Chain.Model.WeiXinNews> list = new List<Chain.Model.WeiXinNews>();
            Chain.Model.WeiXinNews model = new Chain.Model.WeiXinNews();
            model.NewsTitle = NewsTitle;
            model.NewsDesc = NewsDesc;
            model.NewsUrlFirst = NewsUrlFirst;
            model.NewsUrlSecond = NewsUrlSecond;
            list.Add(model);
            return list;
        }

        private string GetRandomCode()
        {
            Random random = new Random();
            string result = string.Empty;
            for (int i = 0; i < 4; i++)
            {
                result = result + random.Next(0, 10);
            }
            return result;
        }

        private void GiveWeiXinLodAssignment(string RecordContent, int RecordContentType, string StatusCode, string RandomCode, int ErrorTimes)
        {
            this.weiXinLogModel.MemWeiXinCard = this.FromUserName;
            this.weiXinLogModel.RecordContent = RecordContent;
            this.weiXinLogModel.RecordContentType = RecordContentType;
            this.weiXinLogModel.StatusCode = StatusCode;
            this.weiXinLogModel.RandomCode = RandomCode;
            this.weiXinLogModel.ErrorTimes = ErrorTimes;
            this.weiXinLogModel.WeiXinLogCreateTime = DateTime.Now;
        }

        public void Log(string logText)
        {
            try
            {
                string fullPath = this.Server.MapPath("~/Upload/Log/" + string.Concat(new object[] { DateTime.Now.ToShortDateString().Replace("-", ""), "_", DateTime.Now.Hour, "_", DateTime.Now.Minute / 10 }) + ".txt");
                logText = DateTime.Now.ToString() + "\r\n" + logText;
                File.AppendAllText(fullPath, logText + "\r\n\r\n");
            }
            catch (Exception ex)
            {
                PubFunction.SaveSysLog(1, 4, "微信接口错误2", ex.ToString() + "  " + ex.StackTrace.ToString(), 1, DateTime.Now, PubFunction.ipAdress);
            }
        }

        public int MemRegister(bool isSMSVcode)
        {
            Chain.Model.Mem memModel = new Chain.Model.Mem();
            if (!isSMSVcode)
            {
                memModel.MemCard = this.Content;
            }
            else
            {
                memModel.MemCard = this.weiXinLogModel.RecordContent;
            }
            memModel.MemPassword = DESEncrypt.Encrypt("");
            memModel.MemName = "";
            memModel.MemSex = true;
            memModel.MemIdentityCard = "";
            if (!isSMSVcode)
            {
                memModel.MemMobile = this.Content;
            }
            else
            {
                memModel.MemMobile = this.weiXinLogModel.RecordContent;
            }
            memModel.MemPhoto = "";
            memModel.MemBirthdayType = true;
            memModel.MemBirthday = Convert.ToDateTime("1900-1-1 0:00:00");
            memModel.MemIsPast = false;
            memModel.MemPastTime = Convert.ToDateTime("2900-1-1 0:00:00");
            memModel.MemPoint = 0;
            memModel.MemPointAutomatic = true;
            memModel.MemMoney = 0M;
            memModel.MemEmail = "";
            memModel.MemAddress = "";
            memModel.MemState = 0;
            memModel.MemRecommendID = 0;
            memModel.MemLevelID = 0;
            memModel.MemShopID = 1;
            memModel.MemCreateTime = DateTime.Now;
            memModel.MemRemark = "";
            memModel.MemUserID = 1;
            memModel.MemTelePhone = "";
            memModel.MemWeiXinCard = this.FromUserName;
            memModel.MemQRCode = "";
            memModel.MemProvince = "";
            memModel.MemCity = "";
            memModel.MemCounty = "";
            memModel.MemVillage = "";
            return this.memBll.Add(memModel);
        }

        private void MenuClick()
        {
            this.weiXinLogBLL.Delete(this.FromUserName);
            string EventKey = this.xmlDoc.GetElementsByTagName("EventKey")[0].InnerText.Trim();
            switch (EventKey)
            {
                case "1":
                    this.ApplicationMemberCard();
                    return;

                case "2":
                    this.ConvertWeiXinMember(this.postStr);
                    return;

                case "3":
                    this.WZZ();
                    break;
            }
            if (EventKey == "3")
            {
                this.WZZ();
            }
            else
            {
                this.UnknownTextNewsHandle(this.postStr, EventKey);
            }
        }

        private void NoLastOperating()
        {
            if (this.MsgType == "event")
            {
                this.AttentionNews(this.postStr);
            }
            else if (this.MsgType == "text")
            {
                this.TextNews();
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1.0);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";
            this.Request = context.Request;
            this.Response = context.Response;
            this.Server = context.Server;
            this.Context = context;
            try
            {
                Stream s = HttpContext.Current.Request.InputStream;
                byte[] b = new byte[s.Length];
                s.Read(b, 0, (int) s.Length);
                this.postStr = Encoding.UTF8.GetString(b);
                this.token = PubFunction.curParameter.strWeiXinToken;
                this.Log(this.postStr);
                if (this.Request.HttpMethod.ToLower() == "get")
                {
                    if ((this.Request.QueryString["signature"] != null) && !string.IsNullOrEmpty(this.Request.QueryString["signature"].ToString()))
                    {
                        this.UrlValid();
                    }
                }
                else
                {
                    this.SendMsg();
                }
            }
            catch (Exception ex)
            {
                this.Log(ex.ToString());
            }
        }

        public void SendMsg()
        {
            this.xmlDoc.LoadXml(this.postStr);
            this.MsgType = this.xmlDoc.GetElementsByTagName("MsgType")[0].InnerText;
            this.FromUserName = this.xmlDoc.GetElementsByTagName("FromUserName")[0].InnerText;
            try
            {
                this.weiXinLogModel = this.weiXinLogBLL.GetModel(this.FromUserName);
                if (this.weiXinLogModel == null)
                {
                    this.weiXinLogModel = new Chain.Model.WeiXinLog();
                    this.NoLastOperating();
                }
                else
                {
                    this.YesLastOperating(this.postStr);
                }
                this.sendXML = (this.sendXML == "") ? ResponseSendStr.Text(this.postStr, this.weiXinRuleBLL.ErrorStr()) : this.sendXML;
            }
            catch (Exception e)
            {
                this.Log(e.ToString());
            }
            finally
            {
                this.Log(this.sendXML);
            }
            this.Response.Write(this.sendXML);
        }

        private void TextNews()
        {
            this.Content = this.xmlDoc.GetElementsByTagName("Content")[0].InnerText.Trim();
            if (this.Content == "1")
            {
                this.ApplicationMemberCard();
            }
            else if (this.Content == "2")
            {
                this.ConvertWeiXinMember(this.postStr);
            }
            else
            {
                this.UnknownTextNewsHandle(this.postStr, this.Content);
            }
        }

        private void UnknownTextNewsHandle(string postStr, string Content)
        {
            this.ruleModel = this.weiXinRuleBLL.GetModelByNewsRuleID(Content);
            if (this.ruleModel != null)
            {
                if (this.ruleModel.RuleNewsType == "text")
                {
                    this.sendXML = ResponseSendStr.Text(postStr, this.ruleModel.RuleContent);
                }
                else if (this.ruleModel.RuleNewsType == "news")
                {
                    List<Chain.Model.WeiXinNews> newsList = new Chain.BLL.WeiXinNews().GetModelList("NewsRuleID=" + this.ruleModel.RuleID);
                    this.sendXML = ResponseSendStr.News(postStr, newsList);
                }
            }
        }

        protected void UrlValid()
        {
            string signature = this.Request.QueryString["signature"].ToString();
            string timestamp = this.Request.QueryString["timestamp"].ToString();
            string nonce = this.Request.QueryString["nonce"].ToString();
            string echostr = this.Request.QueryString["echostr"].ToString();
            List<string> list = new List<string>();
            list.Add(this.token);
            list.Add(timestamp);
            list.Add(nonce);
            list.Sort();
            string newSignature = "";
            foreach (string item in list)
            {
                newSignature = newSignature + item;
            }
            byte[] StrRes = Encoding.UTF8.GetBytes(newSignature);
            StrRes = new SHA1CryptoServiceProvider().ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            if (signature == EnText.ToString())
            {
                this.Response.Write(echostr);
            }
            else
            {
                this.Response.Write("error");
            }
            PubFunction.SaveSysLog(1, 4, "微信接口验证", string.Format("signature={0}&timestamp={1}&nonce={2}&echostr={3}&newSignature={4}&StrRes={5}", new object[] { signature, timestamp, nonce, echostr, newSignature, EnText.ToString() }), 1, DateTime.Now, PubFunction.ipAdress);
        }

        private void WZZ()
        {
            if (this.memBll.GetMemByWeiXinCard(this.FromUserName) == null)
            {
                this.sendXML = ResponseSendStr.Text(this.postStr, "您目前还不是微会员,只有微会员才可以访问微网站");
            }
            else
            {
                string xml = File.ReadAllText(this.Server.MapPath("WWZ.xml"));
                XmlDocument xmlWZZDoc = new XmlDocument();
                xmlWZZDoc.LoadXml(xml);
                string NewsTitle = xmlWZZDoc.GetElementsByTagName("NewsTitle")[0].InnerText;
                string NewsDesc = xmlWZZDoc.GetElementsByTagName("NewsDesc")[0].InnerText;
                string NewsUrlFirst = string.Concat(new object[] { "http://", PubFunction.curParameter.strDoMain, xmlWZZDoc.GetElementsByTagName("NewsUrlFirst")[0].InnerText, "?id=", Guid.NewGuid() });
                string NewsUrlSecond = "http://" + PubFunction.curParameter.strDoMain + xmlWZZDoc.GetElementsByTagName("NewsUrlSecond")[0].InnerText + "?MemWeiXinCard=" + this.FromUserName;
                List<Chain.Model.WeiXinNews> newsList = this.GetModel(NewsTitle, NewsDesc, NewsUrlFirst, NewsUrlSecond);
                this.sendXML = ResponseSendStr.News(this.postStr, newsList);
            }
        }

        private void YesLastOperating(string postStr)
        {
            if (this.MsgType == "event")
            {
                this.AttentionNews(postStr);
            }
            else
            {
                this.weiXinLogModel = this.weiXinLogBLL.GetModel(this.FromUserName);
                string name1 = this.weiXinLogModel.StatusCode;
                if (name1 != null)
                {
                    if (!(name1 == "0"))
                    {
                        if (!(name1 == "1"))
                        {
                            if (!(name1 == "2"))
                            {
                                if (name1 == "3")
                                {
                                    this.Do3ForMemberTransformByReplyTelNumberOrMemberCard();
                                }
                                return;
                            }
                            this.Do2MemberTransform();
                            return;
                        }
                    }
                    else
                    {
                        this.Do0ForApplicationMember();
                        return;
                    }
                    this.Do1ForApplicationMemberByReplyTelNumber();
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
